//
//  HSMainViewHelper.swift
//  HSMainViewControllerHandler
//
//  Created by haisheng huang on 15/12/11.
//  Copyright © 2015年 haisheng huang. All rights reserved.
//

import UIKit

class HSMainViewHelper: NSObject {

    struct Static {
        static var onceToken: Int = 0
        static var instance: HSMainViewHelper? = nil
    }
    
    
    private static var __once: () = {
            Static.instance = HSMainViewHelper()
        }()

//    private var isAddTopSegmentView: Bool? = false
//    private var isAddBottomTabBarView: Bool? = false
//    private var contentAdaptedHeight: CGFloat = 20.0
//    private var contentOriginalY: CGFloat = 20.0
//    var isCreateSliderContentView: Bool? = false {
//        
//        willSet(newValue) {
//        
//        }
//        
//        didSet(oldValue) {
//            
//        }
//    }
    var currentChildViewController: UIViewController? = nil
    var oldChildViewController: UIViewController? = nil
    var contentViewControllers: NSArray? = nil
    var topSegmentView: HSSegmentView? = nil
    var bottomTabBarView: HSTabBarView? = nil
    
    class var sharedInstance: HSMainViewHelper {
        
        _ = HSMainViewHelper.__once
        
        return Static.instance!
    }
    
    func createTopSegmentView(_ mainViewController: HSMainViewController,  titles: NSArray) {
        
        let width: CGFloat = UIScreen.main.bounds.size.width
        var originalY: CGFloat = 20.0
        if mainViewController.navigationController?.navigationBar.isHidden == false {
            originalY = 64.0
        }
        
        let frame: CGRect = CGRect(x: 0.0, y: originalY, width: width, height: 49.0)
        
        let topSegmentView: HSSegmentView = HSSegmentView(frame: frame, titles: titles, icons: nil, style: SegmentStyle.default)
        topSegmentView.delegate = mainViewController
        topSegmentView.setNormalColor(UIColor(white: 0.3, alpha: 1.0), animated: true)
        topSegmentView.setHighlightedColor(UIColor(red: 48.0/255.0, green: 143.0/255.0, blue: 230.0/255.0, alpha: 1.0), animated: true)
        mainViewController.view.addSubview(topSegmentView)
        self.topSegmentView = topSegmentView
        
    }
    
    func createBottomTabBarView(_ mainViewController: HSMainViewController, items: NSArray) {
        
        let width: CGFloat = UIScreen.main.bounds.size.width
        let originalY: CGFloat = mainViewController.view.frame.height - 49.0
        let frame: CGRect = CGRect(x: 0.0, y: originalY, width: width, height: 49.0)
        
        let bottomTabBarView: HSTabBarView = HSTabBarView(frame: frame, items: items, barStyle: HSTabItemStyle.titleAndIcon, selectedIndex: 0)
        bottomTabBarView.delegate = mainViewController
        mainViewController.view.addSubview(bottomTabBarView)
        self.bottomTabBarView = bottomTabBarView

    }
    
    //可滑动
    func createSliderContentView(_ mainViewController: HSMainViewController, viewControllers: NSArray?) {
        
        var contentHeight: CGFloat = mainViewController.view.frame.height
        var contentOriginalY: CGFloat = 20.0
        
        if mainViewController.navigationController?.navigationBar.isHidden == false {
            contentOriginalY = 64.0
            contentHeight -= 64.0
        } else {
            contentOriginalY = 20.0
            contentHeight -= 20.0
        }
        
        if self.topSegmentView != nil {
            contentHeight -= (self.topSegmentView?.frame.height)!
            contentOriginalY += (self.topSegmentView?.frame.height)!
        }
        
        if self.bottomTabBarView != nil {
            contentHeight -= (self.bottomTabBarView?.frame.height)!
        }
        
        let scrollView: UIScrollView = UIScrollView()
        scrollView.frame = CGRect(x: 0.0, y: contentOriginalY, width: mainViewController.view.frame.width, height: contentHeight)
        //print("content view height \(contentHeight)")
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.isPagingEnabled = true //切换必须设置
        scrollView.delegate = mainViewController
        //        scrollView.layer.borderWidth = 0.5
        //        scrollView.layer.borderColor = UIColor.redColor().CGColor
        mainViewController.view.addSubview(scrollView)
        mainViewController.contentView = scrollView
        
        if viewControllers != nil {
            
            for i in 0...viewControllers!.count - 1 {
                
                let viewController = viewControllers![i] as! UIViewController
                
                if i == 0 {
                    self.currentChildViewController = viewController
                }
                
                var frame = viewController.view.frame
                frame.origin.x = scrollView.frame.width * CGFloat(i)
                frame.size = scrollView.frame.size
                viewController.view.frame = frame
                scrollView.addSubview(viewController.view)
            }
            
            let contentSize = CGSize(width: scrollView.frame.width * CGFloat((viewControllers?.count)!), height: scrollView.frame.height)
            scrollView.contentSize = contentSize
            
            self.contentViewControllers = viewControllers
            
        }
        
    }
    
    func moveToNextView(_ scrollView: UIScrollView, index: Int, completion: ((Bool) -> Void)) {
        
        self.viewControllerTransitionHandler(index)
        
        //print("contentOffset: \(scrollView.contentOffset.x)")
        var rect: CGRect = scrollView.frame
        rect.origin.x = scrollView.frame.width * CGFloat(index)
        scrollView.contentOffset.x = scrollView.frame.width * CGFloat(index)
        //scrollView.scrollRectToVisible(rect, animated: true)
        completion(true)
    }
    
    func setTopSegmentViewHighlightItemAtIndex(_ index: Int) {
        
        self.topSegmentView?.setHighlightedItemAtIndex(index)
        
    }
    
    func viewControllerTransitionHandler(_ index: Int) {
        
        self.oldChildViewController = self.currentChildViewController
        
        if self.oldChildViewController != nil {
            self.oldChildViewController?.viewWillDisappear(true)
            //可以继续添加viewDidDisappear
        }
        
        self.currentChildViewController = self.contentViewControllers![index] as? UIViewController
        //需要viewController添加生命周期的viewWillAppear和viewDidAppear的在下面添加即可
        self.currentChildViewController?.viewWillAppear(true)
        self.currentChildViewController?.viewDidAppear(true)
        
    }
    
    //
    func addChildViewController(_ mainViewController:UIViewController, childViewController: UIViewController?, removeOldChildViewController oldChildViewController: UIViewController?) {
        
        guard childViewController != nil else { return }
        
        if oldChildViewController != nil {
            
            mainViewController.transition(from: oldChildViewController!,
                to: childViewController!,
                duration: 0,
                options: UIViewAnimationOptions(),
                animations: { animations in print("animations") },
                completion: { finished in print("completion") })
            
        } else {
           
            
        }
        
    }
}
